#import "SDCaseManageController.h"
#import "SDTableViewHaedView.h"
#import "SDCaseManageCell.h"
#import "SDAddCaseView.h"

#define kFilePath(fileName) ([NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:fileName])


@interface SDCaseManageController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,weak)UITableView * tabaleView;
@property (nonatomic,weak)UIButton * addButton;
@property (nonatomic,strong)NSMutableArray<SDCaseManageModel *>* array;

@end

@implementation SDCaseManageController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self showCaseManage];
    
}

- (void)showCaseManage{
    
    UITableView * tale= [[UITableView alloc]init];
    [self.view addSubview:tale];
    
    self.tabaleView = tale;
	
    [self.tabaleView registerClass:[SDCaseManageCell class] forCellReuseIdentifier:@"cell"];
    
    self.tabaleView.rowHeight = UITableViewAutomaticDimension;
    self.tabaleView.estimatedRowHeight = 200;
    
    self.tabaleView.delegate =self;
    self.tabaleView.dataSource=self;
    
    self.tabaleView.tableFooterView = [[UIView alloc]init];
    
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"jiantou"] style:UIBarButtonItemStylePlain target:nil action:nil];

    
    
    UIButton * buttton = [[UIButton alloc]init];
    
    [buttton setTitle:@"添加病例" forState:UIControlStateNormal];
    
    [buttton setBackgroundColor:[UIColor colorWithRed:0.0902 green:0.6902 blue:0.6824 alpha:1.0]];
    
    [buttton addTarget:self action:@selector(clickAddButton) forControlEvents:UIControlEventTouchUpInside];
    
    [buttton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
    
    [self.view addSubview:buttton];
    
    [buttton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.offset(10);
        make.bottom.trailing.offset(-10);
        make.height.offset(50);
    }];
    
    
    
    [self.tabaleView mas_makeConstraints:^(MASConstraintMaker *make) {
        //
        //        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
        make.top.leading.trailing.offset(0);
        make.bottom.mas_equalTo(buttton.mas_top).offset(0);
    }];
}

- (void)clickAddButton{
    
    SDAddCaseView * addCaseView = [[SDAddCaseView alloc]init];
    
    addCaseView.modeBlook = ^(SDCaseManageModel * caseModle){
<<<<<<< HEAD
	    
        [self.array addObject:caseModle];
	    
        [NSKeyedArchiver archiveRootObject:self.array toFile:kFilePath(@"dd.data")];
        [self.tabaleView reloadData];
=======
        [self.array insertObject:caseModle atIndex:0];
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [NSThread sleepForTimeInterval:10];
            [NSKeyedArchiver archiveRootObject:self.array toFile:kFilePath(@"dd.data")];
        });

        NSIndexPath * indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
        [self.tabaleView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
        [self.tabaleView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
>>>>>>> 6532234cc7e9aaef361899227fb1d7bbe9c20357
    };
    
    [self.navigationController pushViewController:addCaseView animated:YES];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SDCaseManageCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    cell.caseManageModel = self.array[indexPath.row];
    
    return cell;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    
    return 160;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    SDTableViewHaedView * ha= [[SDTableViewHaedView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    
    return ha;
}


-(NSMutableArray<SDCaseManageModel *> *)array{

    if (_array == nil) {
        _array = [NSKeyedUnarchiver unarchiveObjectWithFile:kFilePath(@"dd.data")];
        
        if (_array == nil) {
            _array = [NSMutableArray array];
        }
    }

    return _array;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
<<<<<<< HEAD
	
=======
    
>>>>>>> 6532234cc7e9aaef361899227fb1d7bbe9c20357
    [self.array removeObject:self.array[indexPath.row]];
//    dispatch_async(dispatch_get_global_queue(0, 0), ^{
//        [NSThread sleepForTimeInterval:20];
    [NSKeyedArchiver archiveRootObject:self.array toFile:kFilePath(@"dd.data")];
//    });
    [self.tabaleView reloadData];
<<<<<<< HEAD
	
=======

    
>>>>>>> 6532234cc7e9aaef361899227fb1d7bbe9c20357
}


@end
